var prevPage, nextPage, feedArchive, startPage = 0;

function cropFeed(a, b) {
        var showFeed = a.split("<");
        for (var i = 0; i < showFeed.length; i++) {
                if (showFeed[i].indexOf(">") != -1) {
                        showFeed[i] = showFeed[i].substring(showFeed[i].indexOf(">") + 1, showFeed[i].length);
                }
        }
        showFeed = showFeed.join(" ");
        showFeed = showFeed.substring(0, b - 1);
        return showFeed;
}

function showFeedList(json) {
        var entry, postTitle, postUrl, postImage, postContent, commentNum;
        var skeleton = "";
        prevPage = "";
        nextPage = "";
        for (var i = 0; i < feedNum; i++) {
                if (i == json.feed.entry.length) {
                        break;
                }
                entry = json.feed.entry[i];
                postTitle = entry.title.$t;
                for (var j = 0; j < entry.link.length; j++) {
                        if (entry.link[j].rel == 'alternate') {
                                postUrl = entry.link[j].href;
                                break;
                        }
                }
                for (var k = 0; k < json.feed.link.length; k++) {
                        if (json.feed.link[k].rel == 'prevPageious') {
                                prevPage = json.feed.link[k].href;
                        }
                        if (json.feed.link[k].rel == 'next') {
                                nextPage = json.feed.link[k].href;
                        }
                }
                for (var l = 0; l < entry.link.length; l++) {
                        if (entry.link[l].rel == 'replies' && entry.link[l].type == 'text/html') {
                                commentNum = entry.link[l].title;
                                break;
                        }
                }
                if ("content" in entry) {
                        postContent = entry.content.$t;
                } else if ("summary" in entry) {
                        postContent = entry.summary.$t;
                } else {
                        postContent = "";
                }
                if ("media$thumbnail" in entry) {
                        postImage = entry.media$thumbnail.url.replace(/\/s[0-9]+\-c/, "\/s" + feedThumbWidth + "-c");
                } else {
                        postImage = noImage.replace(/\/s[0-9]+\-c/, "\/s" + feedThumbWidth + "-c");
                }
                skeleton += "<li class='news-title'>";
                skeleton += "<a class='judul-arsip' href='" + postUrl + "' target='_blank'>" + postTitle + "</a>";
                skeleton += "<a href='" + postUrl + "' target='_blank'><img style='width:" + feedThumbWidth + "px;height:" + feedThumbWidth + "px;' src='" + postImage + "' alt='thumbnail' /></a>";
                skeleton += "<div class='news-text'>" + cropFeed(postContent, feedChars) + "&hellip;<br style='clear:both;'/><br/><a class='button small rmore' href='" + postUrl + "' target='_blank'>" + readMoreText + "</a> <a class='button small rmore' href='" + postUrl + "#comments' target='_blank'>" + commentNum + "</a></div>";
                skeleton += "</li>";
        }
        document.getElementById("feedContainer").innerHTML = document.getElementById("feedContainer").innerHTML + skeleton;
        skeleton = "";
        if (nextPage) {
                skeleton += "<a href='javascript:feedNav(1);' class='lanjut'>" + navText + "</a>";
        } else {
                skeleton += "Habis";
        }
        document.getElementById("feedNav").innerHTML = skeleton;
}

function feedNav(archive) {
        var p, param;
        if (archive == -1) {
                p = prevPage.indexOf("?");
                param = prevPage.substring(p);
        } else if (archive == 1) {
                p = nextPage.indexOf("?");
                param = nextPage.substring(p);
        } else {
                param = "?start-index=1&max-results=" + feedNum + "&orderby=published&alt=json-in-script";
        }
        param += "&callback=showFeedList";
        updateScript(param);
}

function updateScript(param) {
        if (startPage == 1) {
                removerscript();
        }
        document.getElementById("feedNav").innerHTML = loadingElement;
        if (labelName !== false) {
                feedArchive = archiveBlog + "/feeds/posts/default/-/" + labelName + param;
        } else {
                feedArchive = archiveBlog + "/feeds/posts/default" + param;
        }
        var toc_script = document.createElement('script');
        toc_script.type = 'text/javascript';
        toc_script.src = feedArchive;
        toc_script.id = 'LABELTEMPORAL';
        document.getElementsByTagName('head')[0].appendChild(toc_script);
        startPage = 1;
}

function removerscript() {
        var elemen = document.getElementById("LABELTEMPORAL");
        var parent = elemen.parentNode;
        parent.removeChild(elemen);
}

window.onload = function() {feedNav(0);};